package com.news.service.impl;

import com.news.dao.NewsDao;
import com.news.model.News;
//import com.news.model.Sort;
import com.news.model.Sort;
import com.news.service.NewsService;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;

public class NewsServiceImpl implements NewsService {
    private NewsDao newsDao = new NewsDao();
    
    @Override
    public List<News> getRecommendNews(int sortId) {
        return newsDao.getRecommendNews(sortId);
    }
    
    @Override
    public List<News> getFocusNews(int sortId) {
        return newsDao.getFocusNews(sortId);
    }
    
    @Override
    public List<News> getDeepNews(int sortId) {
        return newsDao.getDeepNews(sortId);
    }
    
    @Override
    public List<News> getNewsList(int sortId, int limit) {
        return newsDao.getNewsList(sortId, limit);
    }
    
    @Override
    public List<News> getHotNews(int sortId) {
        return newsDao.getHotNews(sortId);
    }
    
    @Override
    public News getNewsById(int newsId) {
        return newsDao.getNewsById(newsId);
    }
    
    @Override
    public List<News> getRelatedNews(int sortId, int currentNewsId) {
        return newsDao.getRelatedNews(sortId, currentNewsId);
    }
    
    @Override
    public List<News> searchNews(String keyword) {
        return newsDao.searchNews(keyword);
    }
    
    @Override
    public int getNewsCount(String keyword) {
        return newsDao.getNewsCount(keyword);
    }
    
    @Override
    public List<News> getNewsByPage(int page, int pageSize, String keyword) {
        int offset = (page - 1) * pageSize;
        return newsDao.getNewsByPage(offset, pageSize, keyword);
    }
    
    @Override
    public List<Sort> getAllSorts() {
        return newsDao.getAllSorts();
    }
    
    @Override
    public boolean addNews(News news, String imagePath) {
        if (imagePath != null && !imagePath.isEmpty()) {
            news.setImg_url(imagePath);
        }
        news.setNewTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return newsDao.addNews(news);
    }
    
    @Override
    public boolean updateNews(News news, String imagePath, boolean removeImage) {
        if (removeImage) {
            news.setImg_url(null);
        } else if (imagePath != null && !imagePath.isEmpty()) {
            news.setImg_url(imagePath);
        }
        return newsDao.updateNews(news);
    }
    
    @Override
    public boolean deleteNews(int newsId) {
        return newsDao.deleteNews(newsId);
    }
    
    @Override
    public int getTotalNewsCount() {
        return newsDao.getTotalNewsCount();
    }
    
    @Override
    public int getTotalUserCount() {
        return newsDao.getTotalUserCount();
    }
    
    @Override
    public int getTotalSortCount() {
        return newsDao.getTotalSortCount();
    }
    
    @Override
    public boolean deleteSort(int sortId) {
        return newsDao.deleteSort(sortId);
    }
    
    @Override
    public int getNewsBySortId(int sortId) {
        return newsDao.getNewsBySortId(sortId);
    }
    
    @Override
    public List<Sort> getSortsByPage(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        return newsDao.getSortsByPage(offset, pageSize);
    }
    
    @Override
    public Sort getSortById(int sortId) {
        return newsDao.getSortById(sortId);
    }
    
    @Override
    public boolean addSort(Sort sort) {
        return newsDao.addSort(sort);
    }
    
    @Override
    public boolean updateSort(Sort sort) {
        return newsDao.updateSort(sort);
    }
    
    @Override
    public List<Sort> searchSortsByPage(String keyword, int currentPage, int pageSize) {
        int offset = (currentPage - 1) * pageSize;
        List<Sort> sortList = newsDao.searchSortsByPage(keyword, offset, pageSize);
        // 为每个分类获取新闻数量
        for (Sort sort : sortList) {
            int newsCount = getNewsBySortId(sort.getSort_id());
            sort.setNewsCount(newsCount);
        }
        return sortList;
    }
    
    @Override
    public int getTotalSearchSortCount(String keyword) {
        return newsDao.getTotalSearchSortCount(keyword);
    }
} 